let arr = [
  [1, 2, 3, 4, 5, [21, 22]],
  [6, 7, 8, 9, 10],
  [11, 12, 13, 14, 15],
  [16, 17, 18, 19, 20]
]


Array.prototype.seleFlat = function (dept = 1) {
  const arr = Array.prototype.slice.call(this)

  if (dept === 0) return arr

  return arr.reduce((pre, cur) => {
    if (Array.isArray(cur)) {
      return [...pre, ...Array.prototype.slice.call(cur, dept - 1)]
    } else {
      return [...pre, cur]
    }
  }, [])
}

// console.log(arr.seleFlat(2));


function flat(arr, res = []) {
  if (Array.isArray(arr)) {
    res.push(flat(arr));
  } else {
    res.push(arr);
  }
}




let a = [1, 2, 3, 4, [5, 6, 7, 8, [9, 10]]]
function flatV4(arr) {
  let res = []

  arr.forEach(item => {
    if (Array.isArray(item)) {
      res = [...res, ...flatV4(item)]
    } else {
      res.push(item)
    }
  })

  return res;
}

console.log(flatV4(a));